import axios from "axios";
import store from "../store";
import {getToken} from "./auth";
import baseconfig from '../../baseconfig'
const axiosIns = axios.create({
  // baseURL: '/api', 默认重写替换为本地启动端口，改为动态获取。
  baseURL: baseconfig[import.meta.env.MODE].baseUrl,
  timeout: 10000,
  // headers: {'X-Custom-Header': 'foobar'}
})

// Add a request interceptor
axiosIns.interceptors.request.use(config => {
  // Do something before request is sent
  if(store.getters.token){
    config.headers['Authorization'] = getToken()
  }
  return config;
}, error => {
  // Do something with request error
  return Promise.reject(error);
})

// Add a response interceptor
axiosIns.interceptors.response.use(response => {
  // Do something with response data
  return response;
}, error => {
  // Do something with response error
  return Promise.reject(error);
})

export default axiosIns
